local skel = fk.CreateSkill {
  name = "rmt__sichan",
  dynamic_desc = function (self, player, lang)
    local mark = player:getMark("rmt__sichan_removed")
    if mark > 0 then
      return "rmt__sichan"..mark
    end
  end,
}

local DIY = require "packages.diy_utility.diy_utility"

skel:addEffect(fk.TurnEnd, {
  anim_type = "switch",
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(skel.name) then return false end
    local index = DIY.getSwitchState(player, skel.name)
    if table.contains(player:getTableMark("rmt__sichan-round"), index) then return false end
    return (#player.room.logic:getEventsOfScope(GameEvent.UseCard, 1, function(e)
        return e.data.from == player
      end, Player.HistoryTurn) > 0 or
      #player.room.logic:getEventsOfScope(GameEvent.RespondCard, 1, function(e)
        return e.data.from == player
      end, Player.HistoryTurn) > 0)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local index = DIY.getSwitchState(player, skel.name)
    local prompt = "#rmt__sichan"..index.."-invoke"
    if index < 3 and index + player:getMark("rmt__sichan_removed") == 3 then
      prompt = prompt.."2"
    end
    local tos = room:askToChoosePlayers(player, {
      targets = room.alive_players,
      max_num = 1, min_num = 1, skill_name = skel.name,
      prompt = prompt,
    })
    if #tos > 0 then
      event:setCostData(self, {tos = tos})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    local n = DIY.getSwitchState(player, skel.name)
    room:addTableMark(player, "rmt__sichan-round", n)
    local will_discard, will_remove = false, false
    if n + player:getMark("rmt__sichan_removed") == 3 then
      will_discard = true
      if player:getMark("rmt__sichan_removed") < 2 then
        room:addPlayerMark(player, "rmt__sichan_removed", 1)
        will_remove = true
      end
      DIY.setSwitchState(player, skel.name, 1, 3 - player:getMark("rmt__sichan_removed"))
    else
      DIY.setSwitchState(player, skel.name, n + 1, 3 - player:getMark("rmt__sichan_removed"))
    end
    to:drawCards(n, skel.name)
    if not to.dead and will_discard then
      room:askToDiscard(to,  {max_num = 3, min_num = 3, skill_name = skel.name, include_equip = true, cancelable = false})
    end
    if not player.dead and will_remove then
      player:drawCards(n, skel.name)
    end
  end,
})

skel:addAcquireEffect(function (self, player, is_start)
  DIY.setSwitchState(player, self.name, 1, 3)
end)

skel:addLoseEffect(function (self, player, is_death)
  player.room:setPlayerMark(player, "rmt__sichan_removed", 0)
  player.room:setPlayerMark(player, "rmt__sichan-round", 0)
  DIY.removeSwitchSkill(player, self.name)
end)

Fk:loadTranslationTable{
  ["rmt__sichan"] = "斯缠",
  [":rmt__sichan"] = "转换技，每轮每项限一次，当你使用或打出过牌的回合结束时，你可以令一名角色摸①一②二③三张牌。若此次发动为最后一项，该角色弃置三张牌，然后若转换项不唯一，你执行并移除此项。",
  [":rmt__sichan1"] = "转换技，每轮每项限一次，当你使用或打出过牌的回合结束时，你可以令一名角色摸①一②二。若此次发动为最后一项，该角色弃置三张牌，然后若转换项不唯一，你执行并移除此项。",
  [":rmt__sichan2"] = "转换技，每轮每项限一次，当你使用或打出过牌的回合结束时，你可以令一名角色摸一张牌，然后该角色弃置三张牌。",


  ["#rmt__sichan1-invoke"] = "斯缠：你可以令一名角色摸一张牌",
  ["#rmt__sichan1-invoke2"] = "斯缠：你可以令一名角色摸一张牌并弃置三张牌",
  ["#rmt__sichan2-invoke"] = "斯缠：你可以令一名角色摸两张牌",
  ["#rmt__sichan2-invoke2"] = "斯缠：你可以令一名角色摸两张牌并弃置三张牌",
  ["#rmt__sichan3-invoke"] = "斯缠：你可以令一名角色摸三张牌并弃置三张牌",

  ["$rmt__sichan1"] = "盛怒之师难破，盛怒之势可挫。",
  ["$rmt__sichan2"] = "归师勿遏，穷寇勿迫。",
  ["$rmt__sichan3"] = "急攻则败散其众，缓之则其谋得成。",
}

return skel
